import type { TableColumn } from '@/framework/components/core/super-table';
import { Tag } from 'ant-design-vue';
import dayjs from 'dayjs';
import { ref } from 'vue';
import { commonCode } from '@/api/common/common_code';
export const deviceType = ref();
/**
 * 设备状态
 */
export const deviceState = {
  0: '未注册',
  1: '已注册',
};
/**
 * columns
 */
export type TableListItem = {
  deviceId;
  online;
  name;
  sn;
  type;
  status;
  createTime;
};
export type TableColumnItem = TableColumn<TableListItem>;

export const baseColumns: TableColumnItem[] = [
  {
    type: 'seq',
    title: '序号',
    width: 60,
  },
  {
    title: '在线',
    field: 'online',
    slots: {
      default({ row }) {
        return <Tag color={row.online ? 'green' : 'red'}>{row.online ? '在线' : '离线'}</Tag>;
      },
    },
    hideInSearch: true,
  },
  {
    title: '设备名称',
    field: 'name',
  },
  {
    title: '设备编号',
    field: 'sn',
  },
  {
    title: '设备类型',
    field: 'type',
    formatter: ({ cellValue }) => {
      return deviceType.value?.find((item) => item.value === cellValue)?.label ?? '';
    },
    formItemProps: {
      component: 'Select',
      componentProps: {
        request: async () => {
          const res = await commonCode('device_type');
          return (deviceType.value = res.result.map((item) => ({
            label: item.name,
            value: Number(item.code),
          })));
        },
      },
    },
  },
  {
    title: '设备状态',
    field: 'status',
    slots: {
      default({ row }) {
        return <Tag color={row.status ? 'green' : 'red'}>{deviceState[row.status]}</Tag>;
      },
    },
    hideInSearch: true,
  },
  {
    title: '创建时间',
    field: 'createTime',
    formatter: ({ cellValue }) => {
      return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss');
    },
    hideInSearch: true,
  },
];
